﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace 三国杀
{
    public partial class QQSearch : Form
    {
        public QQSearch()
        {
            InitializeComponent();
            this.MouseDown += QQSearc_MouseDown;
            this.MouseUp += QQSearc_MouseUp;
            this.MouseMove += QQSearc_MouseMove;
        }

        Point mousePosition;

        Point formPosition;

        bool isMouseDown = false;//是否按下


        private void QQSearc_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;

            mousePosition = Control.MousePosition;  //鼠标位置

            formPosition = this.Location;   //窗体左上角位置
        }

        private void QQSearc_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void QQSearc_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePoint = Control.MousePosition;   //移动的位置
            if (isMouseDown)
            {
                this.Location = new Point(mousePoint.X - mousePosition.X + formPosition.X, mousePoint.Y - mousePosition.Y + formPosition.Y);
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtwhere.Text))
            {
                MessageBox.Show("请输入要查询的条件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                show();
            }
            catch (SqlException ex)
            {
                MessageBox.Show($"数据库查询出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void AddUser_Load(object sender, EventArgs e)
        {
        }



        #region 添加好友弹窗信息传递
        public int imagenum0;
        public string Name0;
        public string Qm0;

        public int imagenum1;
        public string Name1;
        public string Qm1;

        public int imagenum2;
        public string Name2;
        public string Qm2;

        public int imagenum3;
        public string Name3;
        public string Qm3;

        public int imagenum;
        public string Name;
        public string Qm;
        #endregion

        //显示结果
        public void show()
        {
            DB db = new DB();
            string sql;
            int resultCount = 0;

            try
            {
                db.dbOpen(); // 打开数据库

                // 先检查是否是纯数字(可能是QQ号)
                bool isNumber = Regex.IsMatch(this.txtwhere.Text.Trim(), "^\\d+$");

                if (isNumber)
                {
                    // 如果是数字，先检查是否是QQ号
                    string checkSql = "SELECT COUNT(*) FROM QQUsers WHERE QQ = @QQ";
                    SqlCommand checkCmd = new SqlCommand(checkSql, db.Conn);
                    checkCmd.Parameters.AddWithValue("@QQ", this.txtwhere.Text.Trim());

                    int count = (int)checkCmd.ExecuteScalar();

                    if (count > 0)
                    {
                        sql = "SELECT * FROM QQUsers WHERE QQ = @QQ";
                    }
                    else
                    {
                        // 如果不是QQ号，按名称搜索
                        sql = "SELECT * FROM QQUsers WHERE Name LIKE @Name";
                    }
                }
                else
                {
                    // 不是数字，直接按名称搜索
                    sql = "SELECT * FROM QQUsers WHERE Name LIKE @Name";
                }

                SqlCommand comm = new SqlCommand(sql, db.Conn);

                // 添加参数防止SQL注入
                if (sql.Contains("@QQ"))
                {
                    comm.Parameters.AddWithValue("@QQ", this.txtwhere.Text.Trim());
                }
                if (sql.Contains("@Name"))
                {
                    comm.Parameters.AddWithValue("@Name", "%" + this.txtwhere.Text.Trim() + "%");
                }

                // 先清空现有显示
                ClearSearchResults();

                int index = 0;
                SqlDataReader rd = comm.ExecuteReader();
                while (rd.Read())
                {
                    if (index == 0)
                    {
                        imagenum0 = Convert.ToInt32(rd["imagenum"]);
                        Name0 = rd["Name"].ToString();
                        Qm0 = rd["qianming"].ToString();

                        this.p1.BackgroundImage = QQimage.Images[imagenum0];
                        this.name1.Text = Name0;
                        this.qm1.Text = Qm0;
                        this.p1.Visible = true;
                        this.name1.Visible = true;
                        this.qm1.Visible = true;
                    }
                    else if (index == 1)
                    {
                        imagenum1 = Convert.ToInt32(rd["imagenum"]);
                        Name1 = rd["Name"].ToString();
                        Qm1 = rd["qianming"].ToString();

                        this.p2.BackgroundImage = QQimage.Images[imagenum1];
                        this.name2.Text = Name1;
                        this.qm2.Text = Qm1;
                        this.p2.Visible = true;
                        this.name2.Visible = true;
                        this.qm2.Visible = true;
                    }
                    else if (index == 2)
                    {
                        imagenum2 = Convert.ToInt32(rd["imagenum"]);
                        Name2 = rd["Name"].ToString();
                        Qm2 = rd["qianming"].ToString();

                        this.p3.BackgroundImage = QQimage.Images[imagenum2];
                        this.name3.Text = Name2;
                        this.qm3.Text = Qm2;
                        this.p3.Visible = true;
                        this.name3.Visible = true;
                        this.qm3.Visible = true;
                    }
                    else if (index == 3)
                    {
                        imagenum3 = Convert.ToInt32(rd["imagenum"]);
                        Name3 = rd["Name"].ToString();
                        Qm3 = rd["qianming"].ToString();

                        this.p4.BackgroundImage = QQimage.Images[imagenum3];
                        this.name4.Text = Name3;
                        this.qm4.Text = Qm3;
                        this.p4.Visible = true;
                        this.name4.Visible = true;
                        this.qm4.Visible = true;
                    }

                    index++;
                    resultCount++;
                }

                rd.Close();

                // 显示搜索结果信息
                if (resultCount == 0)
                {
                    MessageBox.Show("没有找到匹配的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show($"找到 {resultCount} 个匹配的用户", "搜索结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            finally
            {
                db.dbClose(); // 确保关闭数据库连接
            }
        }

        private void ClearSearchResults()
        {
            this.p1.Visible = false;
            this.p2.Visible = false;
            this.p3.Visible = false;
            this.p4.Visible = false;

            this.name1.Text = "";
            this.name2.Text = "";
            this.name3.Text = "";
            this.name4.Text = "";

            this.qm1.Text = "";
            this.qm2.Text = "";
            this.qm3.Text = "";
            this.qm4.Text = "";
        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void p4_Click(object sender, EventArgs e)
        {
            QQUserAdd QQAdd = new QQUserAdd();
            QQAdd.imagenum = imagenum3;
            QQAdd.Name = Name3;
            QQAdd.Qm = Qm3;
            QQAdd.ShowDialog();
        }

        private void name1_Click(object sender, EventArgs e)
        {

        }

        private void p1_Click(object sender, EventArgs e)
        {
          QQUserAdd QQAdd =  new QQUserAdd();
          QQAdd.imagenum = imagenum0;
          QQAdd.Name = Name0;
          QQAdd.Qm = Qm0;
          QQAdd.ShowDialog();
        }

        private void p2_Click(object sender, EventArgs e)
        {
            QQUserAdd QQAdd = new QQUserAdd();
            QQAdd.imagenum = imagenum1;
            QQAdd.Name = Name1;
            QQAdd.Qm = Qm1;
            QQAdd.ShowDialog();
        }

        private void p3_Click(object sender, EventArgs e)
        {
            QQUserAdd QQAdd = new QQUserAdd();
            QQAdd.imagenum = imagenum2;
            QQAdd.Name = Name2;
            QQAdd.Qm = Qm2;
            QQAdd.ShowDialog();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
